<template>
  <el-dialog title="添加用户" :visible.sync="$store.state.Isdialog" modal @close="dialogClose">
        <el-form 
        :rules="verifyRules" 
        label-width="80px"
        :model="userForm"
        ref="addUser"
        >
          <el-form-item label="用户名称" prop="username">
            <el-input autocomplete="off" v-model="userForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input autocomplete="off" type="password" v-model="userForm.password"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input autocomplete="off" v-model="userForm.email"></el-input>
          </el-form-item>
          <el-form-item label="手机" prop="mobile">
            <el-input autocomplete="off" v-model="userForm.mobile"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="openDialog">取 消</el-button>
          <el-button type="primary" @click="addUserItem">确 定</el-button>
        </div>
      </el-dialog>
</template>

<script>
import {addUser} from '@/api/user'
export default {
  props:{
    userForm:{
      type:Object
    },
    Isdialog:{
      type:Boolean,
      default:false
    }
  },
  data(){
      /* 邮箱正则验证 */
    var checkEmail = (rule, value, callback) => {
      var regEmail = /^[A-Za-z0-9]+([_\.][A-Za-z0-9]+)*@([A-Za-z0-9\-]+\.)+[A-Za-z]{2,6}$/;
      if(regEmail.test(value)){
        return callback();
      }
      /* 如果验证不通过 */
      callback(new Error('请输入合法邮箱'))
    };
    /* 手机号正则验证 */
    var checkPhone = (rule,value,callback) => {
      var regPhone = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      if(regPhone.test(value)){
        return callback();
      }
      /* 如果手机号验证不通过 */
      callback(new Error('请输入正确的手机号码'))
    }
    return {
      verifyRules: {
        username: [
          { message: "请输入用户名字", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" },
        ],
        password:[
          { message: "请输入密码", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 12 个字符", trigger: "blur" },
        ],
        email:[
          { validator:checkEmail, trigger: "blur" },
        ],
        phone:[
          { validator:checkPhone, trigger: "blur" },
        ]
      },
      Fdialog:this.Isdialog
    }
  },
  methods:{
    /* 校验表单信息并发起网络请求 */
    addUserItem() {
      this.$refs.addUser.validate(async valid => 
      {
        const {data:res} = await this.$http.post('users',this.userForm)
        console.log(res);
      })
    },
    // addUserItem() {
    //   this.$refs.addUser.validate( async valid => {
    //     // if(!valid){
    //     //   this.$alert('表单不能为空！','警告',{confirButtonText: '确定'})
    //     // }
    //     /* 发起添加用户网络请求 */
    //    const res = await addUser(this.userForm)
    //    console.log(res);
    //       if(res.data.meta.status !== 201){
    //         this.$message({
    //           type:'warning',
    //           message:res.data.meta.msg,
    //           duration:2000
    //         })
    //         return;
    //       }
    //       /* 关闭表单 */
    //       this.Fdialog = false;
    //       // /* 刷新用户列表 */
    //       this.$store.dispatch("getUser", this.reqData)
    //     })
    // },
    /* 关闭时重置表单 */
    dialogClose() {
      this.$refs.addUser.resetFields()
    },
    openDialog() {
      this.$store.state.Isdialog = !this.$store.state.Isdialog;
    },
  }
}
</script>

<style>

</style>